package com.threeonemeeting.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.threeonemeeting.R;
import com.threeonemeeting.listcache.MeetingNameInfoCache;
import com.threeonemeeting.model.RelativeMeetingListItemInfo;

public class RelativeMeetingListAdapter extends
		ArrayAdapter<RelativeMeetingListItemInfo> {

	private ArrayList<RelativeMeetingListItemInfo> mRelativeMeetingItems = null;

	public RelativeMeetingListAdapter(Activity activity) {
		super(activity, 0);
	}

	public void setRelativeMeetingList(ArrayList<RelativeMeetingListItemInfo> relativeMeetingList) {
		mRelativeMeetingItems = relativeMeetingList;
		notifyDataSetChanged();
	}

	public ArrayList<RelativeMeetingListItemInfo> getRelativeMeetingList() {
		return mRelativeMeetingItems;
	}

	@Override
	public void add(RelativeMeetingListItemInfo relativeMeetingInfo) {
		if (mRelativeMeetingItems == null)
			mRelativeMeetingItems = new ArrayList<RelativeMeetingListItemInfo>();

		mRelativeMeetingItems.add(relativeMeetingInfo);
	}

	public void clear() {
		if (mRelativeMeetingItems != null) {
			mRelativeMeetingItems.clear();
		}
	}

	@Override
	public int getCount() {
		if (mRelativeMeetingItems == null)
			return 0;

		return mRelativeMeetingItems.size();
	}

	@Override
	public RelativeMeetingListItemInfo getItem(int position) {
		return mRelativeMeetingItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		Activity activity = (Activity) getContext();
		RelativeMeetingListItemInfo meetingInfoItemData = getItem(position);

		View rowView = convertView;
		MeetingNameInfoCache viewCache;
		if (rowView == null) {
			LayoutInflater inflater = activity.getLayoutInflater();
			rowView = inflater.inflate(R.layout.lvi_meeting_name_item, null);
			viewCache = new MeetingNameInfoCache(rowView,
					meetingInfoItemData.nMeetingId);
			rowView.setTag(viewCache);
		} else {
			viewCache = (MeetingNameInfoCache) rowView.getTag();
		}

		// 会议标题
		TextView meetingTitleView = viewCache.getMeetingTitleTextView();
		String strMeetingTitle = meetingInfoItemData.strMeetingName;
		if (strMeetingTitle != null && strMeetingTitle.length() > 14) {
			strMeetingTitle = strMeetingTitle.substring(0, 14);
			strMeetingTitle += "...";
		}
		meetingTitleView.setText(strMeetingTitle);

		return rowView;
	}

}
